package Petgoods;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/*
管理员控制系统，只写了注册功能按1注册买家，按2注册卖家
 */
public class Admincontrols {
    public void usercontrol() {
        Adminmeau adminmeau = new Adminmeau();
        adminmeau.meau();                           // 管理员菜单界面

        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        switch (num) {
            case 1:
                registeredbuyeruser();
                break;
            case 2:
                registeredselleruser();
                break;
            default:
                System.out.println("无效输入，已退出");
                break;
        }
    }

    /*
    让买家注册，买家表buyer只有两个字段，名称和余额，注册的时候买家手动输入名称插入到买家表buyer中，第一次注册赠送1000元余额
     */
    private void registeredbuyeruser() {
        JdbcUtil util = new JdbcUtil();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入买家名称：");
        String name = sc.nextLine();
        System.out.println("请输入买家密码：");
        String password = sc.nextLine();
        int balance = 1000;

        String sql = "INSERT INTO buyer VALUES(?,?,?)";
        try {
            Connection conn = util.getConn();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setInt(2, balance);
            ps.setString(3, password);
            int rows = ps.executeUpdate();
            if (rows > 0) {
                System.out.println("欢迎使用本商城，第一次注册赠送您1000余额");
            }
            util.closeResource(null, conn, ps);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /*
     注册卖家，卖家表seller，只有1个字段名称
      */
    private void registeredselleruser() {
        JdbcUtil util = new JdbcUtil();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入卖家名称：");
        String name = sc.nextLine();
        System.out.println("请输入卖家密码：");
        String password = sc.nextLine();

        String sql = "INSERT INTO seller VALUES(?,?)";
        try {
            Connection conn = util.getConn();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, password);
            int rows = ps.executeUpdate();
            if (rows > 0) {
                System.out.println("欢迎使用本商城");
            }
            util.closeResource(null, conn, ps);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}